<?php
/**
 * 测试HTML保存功能
 */
require_once __DIR__ . '/classes/HtmlParser.php';
require_once __DIR__ . '/classes/Logger.php';

echo "<!DOCTYPE html>";
echo "<html><head><meta charset='UTF-8'><title>测试HTML保存</title>";
echo "<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #f5f5f5; }
.container { max-width: 1200px; margin: 0 auto; background: white; padding: 20px; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
h1 { color: #333; }
.success { color: green; font-weight: bold; }
.error { color: red; font-weight: bold; }
.info { color: blue; }
pre { background: #f8f8f8; padding: 15px; border-radius: 5px; overflow-x: auto; white-space: pre-wrap; word-wrap: break-word; }
</style>";
echo "</head><body><div class='container'>";

echo "<h1>🧪 测试HTML保存功能</h1>";

// 测试关键词
$testKeyword = isset($_GET['keyword']) ? $_GET['keyword'] : '人工智能';
echo "<p>测试关键词: <strong>{$testKeyword}</strong></p>";

$searchUrl = "https://wm.m.sm.cn/s?from=wm745640&q=" . urlencode($testKeyword);
echo "<p>搜索URL: <a href='{$searchUrl}' target='_blank'>{$searchUrl}</a></p>";

echo "<hr>";

$logger = new Logger();
$htmlParser = new HtmlParser();

echo "<h2>步骤1: 获取HTML内容</h2>";
$startTime = microtime(true);
$html = $htmlParser->fetchHtml($searchUrl);
$fetchTime = round((microtime(true) - $startTime) * 1000, 2);

if ($html !== false) {
    echo "<p class='success'>✅ 成功获取HTML (耗时: {$fetchTime}ms, 大小: " . strlen($html) . " 字节)</p>";
} else {
    echo "<p class='error'>❌ 获取HTML失败</p>";
    echo "<p>请检查日志文件查看详细错误信息</p>";
}

echo "<h2>步骤2: 保存HTML到文件</h2>";

if ($html !== false) {
    $htmlFile = __DIR__ . '/return.html';
    
    echo "<p>目标文件: <code>{$htmlFile}</code></p>";
    echo "<p>当前目录: <code>" . __DIR__ . "</code></p>";
    echo "<p>当前目录可写: " . (is_writable(__DIR__) ? '<span class="success">是</span>' : '<span class="error">否</span>') . "</p>";
    
    // 尝试保存文件
    $result = @file_put_contents($htmlFile, $html);
    
    if ($result !== false) {
        echo "<p class='success'>✅ 成功保存HTML到文件 ({$result} 字节)</p>";
        echo "<p>文件路径: <code>" . realpath($htmlFile) . "</code></p>";
        echo "<p>文件存在: " . (file_exists($htmlFile) ? '<span class="success">是</span>' : '<span class="error">否</span>') . "</p>";
        echo "<p>文件大小: " . filesize($htmlFile) . " 字节</p>";
        
        echo "<p><a href='return.html' target='_blank' style='color: #007cba; text-decoration: none;'>📄 查看保存的HTML文件</a></p>";
    } else {
        echo "<p class='error'>❌ 保存HTML文件失败</p>";
        
        $error = error_get_last();
        if ($error) {
            echo "<p class='error'>错误信息: " . htmlspecialchars($error['message']) . "</p>";
        }
        
        // 尝试诊断问题
        echo "<h3>诊断信息:</h3>";
        echo "<ul>";
        echo "<li>目录是否存在: " . (is_dir(__DIR__) ? '是' : '否') . "</li>";
        echo "<li>目录是否可写: " . (is_writable(__DIR__) ? '是' : '否') . "</li>";
        echo "<li>目录权限: " . substr(sprintf('%o', fileperms(__DIR__)), -4) . "</li>";
        echo "<li>PHP运行用户: " . (function_exists('posix_getpwuid') ? posix_getpwuid(posix_geteuid())['name'] : 'unknown') . "</li>";
        echo "</ul>";
        
        // 尝试使用临时目录
        echo "<h3>尝试保存到临时目录:</h3>";
        $tempFile = sys_get_temp_dir() . '/return_' . time() . '.html';
        $tempResult = @file_put_contents($tempFile, $html);
        
        if ($tempResult !== false) {
            echo "<p class='success'>✅ 成功保存到临时目录</p>";
            echo "<p>临时文件: <code>{$tempFile}</code></p>";
            echo "<p class='info'>建议: 请手动将文件复制到项目目录，或修改目录权限</p>";
            echo "<pre>chmod 755 " . __DIR__ . "</pre>";
        } else {
            echo "<p class='error'>❌ 临时目录也无法写入</p>";
        }
    }
    
    echo "<h2>步骤3: HTML内容预览</h2>";
    echo "<details><summary>点击查看HTML内容 (前2000字符)</summary>";
    echo "<pre>" . htmlspecialchars(substr($html, 0, 2000)) . "...</pre>";
    echo "</details>";
    
} else {
    echo "<p class='error'>由于HTML获取失败，跳过保存步骤</p>";
}

echo "<hr>";
echo "<p><a href='test_parse.php' style='color: #007cba; margin: 0 10px;'>完整测试工具</a>";
echo "<a href='view_log.php' target='_blank' style='color: #007cba; margin: 0 10px;'>查看日志</a>";
echo "<a href='index.php' style='color: #007cba; margin: 0 10px;'>返回首页</a></p>";

echo "</div></body></html>";
?>
